/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.util;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import rpg.gfx.Bitmap;

/**
 * rothens.tarhely.biz
 * @author Rothens
 */
public class ImageUtil {

	/**
	 * Creates a BufferedImage from the given resource path.
	 * @param path
	 * @return a BufferedImage, or null if not found
	 */
	public static BufferedImage createImageIcon(String path) {
		try {
			return ImageIO.read(ImageUtil.class.getResource(path));
		} catch (IOException ex) {
			System.err.println("Couldn't read image from " + path);

		}
		return null;
	}

	public static Bitmap getSub(BufferedImage img, int row, int col) {
		Bitmap ret = new Bitmap(64, 64);

		img.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, ret.width, ret.height, ret.pixels, 0, ret.width);

		return ret;
	}
	
	public static Bitmap getSub(BufferedImage img, int row, int col, int w, int h) {
		Bitmap ret = new Bitmap(w, h);

		img.getSubimage(col * w, row * h, w, h).getRGB(0, 0, ret.width, ret.height, ret.pixels, 0, ret.width);

		return ret;
	}
}
